package org.jetnuke.modules.admin.survey;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

import org.apache.log4j.Logger;
import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.Poll;
import org.jetnuke.entities.Stories;
import org.jetnuke.util.JetUtil;

public class AddSurvey implements IAction {
	private final static Logger s_log = Logger.getLogger(AddSurvey.class);
	String title;
	String art_title;
	String story, story_ext; int topic, category,acomm;
	public void setArt_title(String art_title) {
		this.art_title = art_title;
	}
	String opt_1, opt_2, opt_3, opt_4, opt_5, opt_6, opt_7, opt_8, opt_9,
			opt_10, opt_11, opt_12, opt_13, opt_14, opt_15;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, org.jetnuke.common.IUserInfo,
	 *      org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		Poll poll = new Poll();
		poll.setTIMESTAMP(new Date());
		poll.setPLANGUAGE("");
		poll.setPOLLTITLE(title);
		poll.setARTID(-1);
		//poll.save();
		int id = DataAccessDriver.getInst().newJPollDAO().addNew(poll);

		Connection cn = null;
		
		try {
			cn = JForumExecutionContext.getConnection();
			
			PreparedStatement ps = cn
					.prepareStatement(SystemGlobals.getSql("PollDataModel.addNew"));
			boolean cont = false;
			if (opt_1.length() > 0) {
				ins(ps, id, 1, opt_1);
				cont = true;
			}
			if (cont == true && opt_2.length() > 0) {
				ins(ps, id, 2, opt_2);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_3.length() > 0) {
				ins(ps, id, 3, opt_3);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_4.length() > 0) {
				ins(ps, id, 4, opt_4);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_5.length() > 0) {
				ins(ps, id, 5, opt_5);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_6.length() > 0) {
				ins(ps, id, 6, opt_6);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_7.length() > 0) {
				ins(ps, id, 7, opt_7);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_8.length() > 0) {
				ins(ps, id, 8, opt_8);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_9.length() > 0) {
				ins(ps, id, 9, opt_9);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_10.length() > 0) {
				ins(ps, id, 10, opt_10);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_11.length() > 0) {
				ins(ps, id, 11, opt_11);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_12.length() > 0) {
				ins(ps, id, 12, opt_12);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_13.length() > 0) {
				ins(ps, id, 13, opt_13);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_14.length() > 0) {
				ins(ps, id, 14, opt_14);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_15.length() > 0) {
				ins(ps, id, 15, opt_15);
				cont = true;
			} else {
				cont = false;
			}
			ps.close();
			//cn.commit();
		} catch (Exception e) {
			s_log.error(e);

			//if (cn != null) {
			//	cn.rollback();
			//}

		} finally {
			//Torque.closeConnection(cn);

		}
		if(!"".equals(art_title)&&!"".equals(story)){
			Stories stor = new Stories();
			stor.setACOMM(acomm);
			stor.setAID(ui.getID());
			stor.setALANGUAGE("");
			stor.setASSOCIATED("");
			stor.setBODYTEXT(story_ext);
			stor.setCATID(category);
			stor.setHOMETEXT(story);
			
			stor.setINFORMANT(ui.getID());
			stor.setNOTES("");
			stor.setHASPOLL(1);
			stor.setPOLLID(id);
			stor.setTIME(new Date());
			stor.setTITLE(art_title);
			stor.setTOPIC(topic);
			int storyID=
				DataAccessDriver.getInst().newStoriesDAO().addNew(stor);
			poll.setARTID(storyID);
			DataAccessDriver.getInst().newJPollDAO().update(poll);
			/*List ls=DataAccessDriver.getInst().newAuthorsModel().getByAID(ai.getAID());
			if(!ls.isEmpty()){
				Author au = (Author)ls.get(0);
				if (au != null) {
					au.setCOUNTER(au.getCOUNTER() + 1);
					DataAccessDriver.getInst().newAuthorsModel().update(au);
					//au.save();
				}
			}*/
		}
		JetUtil.redirect(vc,response,request.getContextPath()+"/do/admin/surveys");
		//pd.setOPTIONTEXT(opt_1);
		//pd.save();
		return null;
	}

	public void ins(PreparedStatement ps, int pollID, int voteID, String txt)
			throws SQLException {
		ps.setInt(1, pollID);
		ps.setString(2, txt);
		ps.setInt(3, voteID);
		ps.execute();
	}

	public void setOpt_1(String opt_1) {
		this.opt_1 = opt_1;
	}

	public void setOpt_10(String opt_10) {
		this.opt_10 = opt_10;
	}

	public void setOpt_11(String opt_11) {
		this.opt_11 = opt_11;
	}

	public void setOpt_12(String opt_12) {
		this.opt_12 = opt_12;
	}

	public void setOpt_13(String opt_13) {
		this.opt_13 = opt_13;
	}

	public void setOpt_14(String opt_14) {
		this.opt_14 = opt_14;
	}

	public void setOpt_15(String opt_15) {
		this.opt_15 = opt_15;
	}

	public void setOpt_2(String opt_2) {
		this.opt_2 = opt_2;
	}

	public void setOpt_3(String opt_3) {
		this.opt_3 = opt_3;
	}

	public void setOpt_4(String opt_4) {
		this.opt_4 = opt_4;
	}

	public void setOpt_5(String opt_5) {
		this.opt_5 = opt_5;
	}

	public void setOpt_6(String opt_6) {
		this.opt_6 = opt_6;
	}

	public void setOpt_7(String opt_7) {
		this.opt_7 = opt_7;
	}

	public void setOpt_8(String opt_8) {
		this.opt_8 = opt_8;
	}

	public void setOpt_9(String opt_9) {
		this.opt_9 = opt_9;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	public void setCategory(int category) {
		this.category = category;
	}
	public void setStory(String story) {
		this.story = story;
	}
	public void setStory_ext(String story_ext) {
		this.story_ext = story_ext;
	}
	public void setTopic(int topic) {
		this.topic = topic;
	}
	public void setAcomm(int acomm) {
		this.acomm = acomm;
	}
}
